// This file is part of OpenTSDB.
// Copyright (C) 2017 TeleDB Development Group.
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 2.1 of the License, or (at your
// option) any later version.  This program is distributed in the hope that it
// will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
// General Public License for more details.  You should have received a copy
// of the GNU Lesser General Public License along with this program.  If not,
// see <http://www.gnu.org/licenses/>.
package net.opentsdb.tsd.service;

import net.opentsdb.core.TSDB;
import net.opentsdb.tsd.HttpQuery;
import net.opentsdb.uid.UniqueId;
import org.hbase.async.DeleteRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.NoSuchElementException;

/**
 * 移除metric
 * */
public class RemoveMetricService extends RpcServiceImpl{
    private static final Logger LOG = LoggerFactory.getLogger(RpcServiceImpl.class);

    public RemoveMetricService(TSDB tsdb, HttpQuery query){
        super(tsdb, query);
    }

    @Override
    public void doGet() {
        doPost();
    }

    @Override
    public void doPost() {
        String metric = String.valueOf(((Map<String,String>)params).get("name"));
        try {
            byte[] uidTable = tsdb.uidTable();
            byte[] uid = tsdb.getUID(UniqueId.UniqueIdType.METRIC, metric);
            final byte[] NAME_FAMILY = "name".getBytes();
            final byte[] qualifier = "metrics".getBytes();

            LOG.info("remove and clean metric: "+metric);

            DeleteRequest metricRequest = new DeleteRequest(uidTable,metric.getBytes());
            DeleteRequest uidRequest = new DeleteRequest(uidTable,uid,NAME_FAMILY,qualifier);
            tsdb.getClient().delete(metricRequest);
            tsdb.getClient().delete(uidRequest);
            // 清除uid缓存
            tsdb.deleteUidAsync("metric", metric);
            LOG.debug("start delete tag table message");
            tsdb.getHbaseWrapper().tagUtils.deleteTagTree(metric);

            LOG.debug("success remove metrics "+metric);
        } catch (NoSuchElementException ee){
            LOG.error("metric: "+metric+" doesn't exists");
            putResponse(true, "metric doesn't exists");
        } catch (IllegalArgumentException e) {
            LOG.error("remove metric: "+metric+" error :"+e.getMessage());
            putErrorResponse(false, "delete metric error:"+e.getMessage());
        }

    }

    @Override
    public void checks() {
        Map<String,String> map = (Map<String,String>)params;

        if(!map.containsKey("type") ||
                !"delete".equals(map.get("type"))
                || !map.containsKey("name")){
            LOG.info("error remove metric request");
            putErrorResponse(false,
                    "error delete type request, ex:{type:'delete', name:'xxxx-xx'}");
        }
    }
}
